package com.example.springbootlocal.utils;

import java.util.List;

/**
 * @author X.I.O
 * @title: Strings
 * @projectName spring-boot-local
 * @description: TODO
 * @date 2021/11/9 17:29
 */
public class Strings {

    public static String getString(Object value){
        return value==null? "" : String.valueOf(value);
    }

    /**
     * 将List<String> 转成 in查询需要的字符串连接
     * @param strings
     * @return
     */
    public static String getStringIn(List<String> strings){
        StringBuilder str=new StringBuilder();
        if(strings==null || strings.size()==0){
            return str.toString();
        }
        int sizeNum=strings.size()-1;
        str.append("(");
        for (int i = 0; i < strings.size(); i++) {
            String s=strings.get(i);
            if(i!=sizeNum){
                str.append("'").append(s).append("'").append(",");
            }else {
                str.append("'").append(s).append("'");
            }
        }
        str.append(")");
        return str.toString();
    }

    public static String getStringJoin(List<String> strings,String sperator){
        StringBuilder str=new StringBuilder();
        if(strings==null || strings.size()==0){
            return str.toString();
        }
        int sizeNum=strings.size()-1;
        for (int i = 0; i < strings.size(); i++) {
            String s=strings.get(i);
            if(i!=sizeNum){
                str.append(s).append(sperator);
            }else {
                str.append(s);
            }
        }
        return str.toString();
    }
}
